package com.yuneec.droneservice.command;

import com.MAVLink.yuneec.msg_mav_rc_cmd_ack;
import com.yuneec.droneservice.model.Aircraft;
import com.yuneec.droneservice.request.IResponse;

/**
 * Created by gjw on 2017/3/27.
 */

public class ScanAircraft extends BaseCommand {
    public ScanAircraft() {
        super(MAV_RC_COMMAND.MAV_RC_CMD_SCAN_AUTOPILOT);
    }

    @Override
    protected short[] body() {
        return null;
    }

    @Override
    public IResponse<Aircraft[]> toResponse(msg_mav_rc_cmd_ack ack) {
        int code = ack.code;
        IResponse<Aircraft[]> response = new IResponse(ids(), code);
        if (code == MAV_RC_ERROR_CODE.MAV_RC_ERR_SUCCESS) {
            try {
                Aircraft[] aircrafts = new Aircraft[ack.params[0] & 0xFF];
                for (int i = 0; i < aircrafts.length; i++) {
                    aircrafts[i].setId(uIntArrayToInt(ack.params, 4 * i + 1));
                }
                response.setParam(aircrafts);
            } catch (Exception e) {
                response.setCode(MAV_RC_ERROR_CODE.MAV_RC_ERR_UNKNOW);
            }
        }
        return response;
    }
}
